package ylyhappy.cn.ideavim.ui.configpreview;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import ylyhappy.cn.ideavim.databinding.ConfigPreviewFragmentBinding;

public class ConfigListFragment extends Fragment {

    private ConfigPreviewFragmentBinding binding;

    private CheckBox old_cb = null;

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        binding = ConfigPreviewFragmentBinding.inflate(inflater, container, false);
        ArrayList<String> mList = new ArrayList<>();
        mList.add("Ylyhappy-2020-10-30");
        mList.add("Yuan-2023-10-30");
        mList.add("IdeaVimBuilder-2024-10-30");
        initActivityRecycler(mList);
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding.configListRecycler.setAdapter(null);
        binding = null;

    }

    /**
     * Building the ConfigList View
     *
     * @param mList dataList
     */
    private void initActivityRecycler(List<String> mList) {
        Context context = binding.getRoot().getContext();
        binding.configListRecycler.setLayoutManager(new LinearLayoutManager(context));
        ConfigItemAdapter configItemAdapter = new ConfigItemAdapter(mList, context);
        configItemAdapter.setmOnItemClickListener(new ConfigItemAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                CheckBox cb = (CheckBox) view;
                if (!cb.equals(old_cb) && old_cb != null) {
                    old_cb.setChecked(false);
                }
                old_cb = cb;
            }

        });
        binding.configListRecycler.setAdapter(configItemAdapter);
    }


}